home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / SAT 2.4.0 / SAT / Tutorial ƒ / Assignment6.p < prev    next >
Encoding:
Text File  |  1997-03-12  |  4.1 KB  |  158 lines  |  [TEXT/PJMM]

  1. program Assignment6;
  2.     uses
  3. {$ifc UNDEFINED THINK_PASCAL}
  4.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, {}
  5.         Events, QuickDrawText,
  6. {$endc}
  7.         SAT;
  8.  
  9.     const
  10.         kSpeed = 5;
  11.     var
  12.         ignore: SpritePtr;
  13.         direction: Integer;
  14.         theSound: Handle;
  15.         score: Longint;
  16.         startTime: Longint;
  17.         doneFlag: Boolean;
  18.  
  19.     procedure HandleSprite (me: SpritePtr);
  20.         var
  21.             event: EventRecord;
  22.     begin
  23. {Now hold on to the hat! I'm using GetOSEvent to get key down events.}
  24. {the keydowns then affect the speed variable in the sprite. That speed}
  25. {is added to the position. Finally, I check the position against the screen}
  26. {borders, and modify the speed in case I reach a border.}
  27.  
  28.         if GetOSEvent(keyDownMask, event) then
  29.             if event.what = keyDown then
  30.                 case char(BAnd(event.message, charCodeMask)) of
  31.                     'a': 
  32.                         me^.speed.h := me^.speed.h - 1;
  33.                     's': 
  34.                         me^.speed.h := me^.speed.h + 1;
  35.                     'w': 
  36.                         me^.speed.v := me^.speed.v - 1;
  37.                     'z': 
  38.                         me^.speed.v := me^.speed.v + 1;
  39.                 end;
  40.  
  41.         me^.position.h := me^.position.h + me^.speed.h;
  42.         me^.position.v := me^.position.v + me^.speed.v;
  43.         if me^.position.h < 0 then
  44.             begin
  45.                 me^.speed.h := abs(me^.speed.h);
  46.                 me^.position.h := 0;
  47.             end;
  48.         if me^.position.h > gSAT.offSizeH - 32 then
  49.             begin
  50.                 me^.speed.h := -abs(me^.speed.h);
  51.                 me^.position.h := gSAT.offSizeH - 32;
  52.             end;
  53.         if me^.position.v < 0 then
  54.             begin
  55.                 me^.speed.v := abs(me^.speed.v);
  56.                 me^.position.v := 0;
  57.             end;
  58.         if me^.position.v > gSAT.offSizeV - 32 then
  59.             begin
  60.                 me^.speed.v := -abs(me^.speed.v);
  61.                 me^.position.v := gSAT.offSizeV - 32;
  62.             end;
  63.     end; {HandleSprite}
  64.  
  65.     procedure SetupSprite (me: SpritePtr);
  66.     begin
  67.         me^.task := @HandleSprite;
  68.         me^.face := SATGetFace(128);
  69.         me^.speed := Point(0);
  70.         SetRect(me^.hotRect, 0, 0, 32, 32);
  71.     end; {SetupSprite}
  72.  
  73.     procedure SetupTarget (me: SpritePtr);
  74.     forward;
  75.  
  76.     procedure HandleTarget (me: SpritePtr);
  77.     begin
  78. {The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead}
  79. {of a hard-coded constant.}
  80.         me^.position.h := me^.position.h + direction;
  81.         if me^.position.h <= 0 then
  82.             direction := kSpeed;
  83.         if me^.position.h >= gSAT.offSizeH - 32 then
  84.             direction := -kSpeed;
  85.     end; {HandleTarget}
  86.  
  87.     procedure HitTarget (me, him: SpritePtr);
  88.         var
  89.             savePort: SATPort;
  90.             r: Rect;
  91.     begin
  92.         if him^.task = @HandleSprite then {Chack what we hit!}
  93.             begin
  94.                 me^.task := nil;
  95.                 ignore := SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), @SetupTarget);
  96. {We could also re-use the old sprite for a new one, if we like.}
  97.                 SATSoundPlay(theSound, 1, true);
  98.  
  99. {Add to the score}
  100.                 score := score + 1;
  101.  
  102. {Draw the score on the screen.}
  103.                 SATGetPort(savePort); {Save port and device!}
  104.                 SATSetPortBackScreen;
  105.                 SetRect(r, 100, 0, 100 + StringWidth(stringof('Caught: ', score : 1)), 15);
  106.                 EraseRect(r);
  107.                 MoveTo(r.left, r.bottom - 3);
  108.                 DrawString(stringof('Caught: ', score : 1));
  109.                 SATBackChanged(r);
  110.                 SATSetPort(savePort); {Always restore!}
  111.  
  112.                 if score = 10 then
  113.                     doneFlag := true;
  114.             end;
  115.     end; {HitTarget}
  116.  
  117.     procedure SetupTarget (me: SpritePtr);
  118.     begin
  119.         me^.task := @HandleTarget;
  120.         me^.hitTask := @HitTarget;
  121.         me^.face := SATGetFace(129);
  122.         SetRect(me^.hotRect, 0, 0, 32, 32);
  123.         direction := kSpeed;
  124.     end; {SetupTarget}
  125.  
  126.     const
  127.         kTicksPerFrame = 2;
  128.     var
  129.         t: Longint;
  130.  
  131. begin
  132. {If we don't use Think Pascal, we must make standard inits ourselves.}
  133. {$ifc UNDEFINED THINK_PASCAL}
  134.     SATInitToolbox;
  135. {$endc}
  136.  
  137.     SATConfigure(false, kVPositionSort, kForwardCollision, 32);
  138.     SATInit(128, 129, 478, 302);
  139.     ignore := SATNewSprite(0, 200, 200, @SetupSprite);
  140.     ignore := SATNewSprite(0, 0, SATRand(gSAT.offSizeV), @SetupTarget);
  141.     theSound := SATGetNamedSound('TestSound');
  142.     score := 0;
  143.     HideCursor;
  144.     startTime := TickCount;
  145.     doneFlag := false;
  146.     while not Button and not doneFlag do
  147.         begin
  148.             t := TickCount;
  149.             SATRun(true);
  150.             while TickCount < t + kTicksPerFrame do
  151.                 ;
  152.         end;
  153.     ShowCursor;
  154. {Extremely simple result report. A real game should, of course, display this in the game}
  155. {window, in a prettier way, perhaps with a high score list, etc.}
  156.     SATReportStr(stringof('Time to catch ', score : 1, ' disks: ', (TickCount - startTime) div 60 : 1, ' seconds.'));
  157.     SATSoundShutup;
  158. end.